package cn.school.demo.ui.login

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.school.demo.pojo.Result
import cn.school.demo.pojo.User
import cn.school.demo.repository.DataRepository
import cn.school.demo.repository.LoginRepository
import com.blankj.utilcode.util.LogUtils

class LoginViewModel : ViewModel() {

    private lateinit var _new: LiveData<Result?>
    val new: LiveData<Result?> get() = _new

    private lateinit var _result: LiveData<Result>
    val result: LiveData<Result> get() = _result

    private lateinit var _getCode: LiveData<String>
    val getCode: LiveData<String> get() = _getCode

    private val _id = MutableLiveData<String>()
    val id: LiveData<String>
        get() = _id

    private val _pwd = MutableLiveData<String>()
    val pwd: LiveData<String>
        get() = _pwd

    private val _phone = MutableLiveData<String>()
    val phone: LiveData<String>
        get() = _phone

    private val _code = MutableLiveData<String>()
    val code: LiveData<String>
        get() = _code

    private val _isGone = MutableLiveData<Boolean>()
    val gone: LiveData<Boolean>
        get() = _isGone


    init {
        _id.value = ""
        _pwd.value = ""
        _phone.value = ""
        _code.value = ""
        _isGone.value = false
    }

    fun setId(newId: String) {
        if (newId != _id.value)
            _id.value = newId
    }

    fun setPwd(newPwd: String) {
        _pwd.value = newPwd
    }

    fun setPhone(newPhone: String) {
        _phone.value = newPhone
    }

    fun setCode(newCode: String) {
        _code.value = newCode
    }

    fun changeView(isGone: Boolean) {
        _isGone.value = isGone
    }

    fun getCodeText(phone: String) {
        _getCode = DataRepository().getCode(phone)
    }

    fun getUser(id: Int, pwd: String) {
        _new = LoginRepository().loginByPwd(id, pwd)
    }

    fun getResult(phone: String) {
        _result = LoginRepository().loginByPhone(phone)
    }

    override fun onCleared() {
        super.onCleared()
        LogUtils.d("LoginViewModel销毁了！")
    }
}